#include "mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(0, 0, 800, 480);
    mPushButton = new QPushButton(this);
    mPushButton->setGeometry(400, 400, 100, 50);
    mPushButton->setText("开启线程");

    worker = new WorkerThread();

    worker2 = new WorkerThread2();
    worker2->moveToThread(&workThread);

    connect(mPushButton, &QPushButton::clicked, this, &MainWindow::clicked);
    connect(this, &MainWindow::startWorker, worker2, &WorkerThread2::workone);
}

MainWindow::~MainWindow()
{
    delete worker;
    delete worker2;
}

void MainWindow::clicked()
{
    qDebug() << "---click start---" << endl;
    worker->start();

    const QString str = "hello";
    if (!workThread.isRunning()) {
        workThread.start();
    }
    emit this->startWorker(str);
    qDebug() << "---click end---" << endl;
}

void WorkerThread::run()
{
    qDebug() << "---start---" << endl;
    sleep(2);
    qDebug() << "---end---" << endl;
}
